﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

//玩家控制器，接收玩家输入，执行响应操作
public class PlayerController : MonoBehaviour
{
    //游戏控制器
    public GameController gameController;
    //瞄准点
    public GameObject sightTarget;
    //触摸灵敏度
    public float sensitivity;
    //瞄具倍率滑块
    public Slider sightSlider;
    //枪械控制器
    public GunController gunController;

    Vector2 lastPosition;

    Touch touchPoint;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //镜头移动向量
        Vector2 deltaPosition = new Vector2(0, 0);
        //移动平台（为调试方便不使用预编译语句）
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            //获取触摸操控时的移动向量,如果有触摸输入且触摸状态为移动（只监控第一个触摸点）
            if (Input.touchCount > 0)
            {
                touchPoint = Input.GetTouch(0);
                //不触摸在UI上时，获取触摸移动向量
                if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    deltaPosition = touchPoint.deltaPosition;
                }
            }
        }
        //Editor
        else
        {
            //获取鼠标操控时的移动向量
            if (Input.GetMouseButtonDown(0))
            {
                Vector2 mousePosition = Input.mousePosition;
                lastPosition = mousePosition;
            }
            if (Input.GetMouseButton(0))
            {
                //不触摸在UI上时，获取鼠标移动向量
                Vector2 mousePosition = Input.mousePosition;
                if (!EventSystem.current.IsPointerOverGameObject())
                {
                    if (lastPosition != null)
                    {
                        deltaPosition = mousePosition - lastPosition;
                    }
                    lastPosition = mousePosition;
                }
            }
        }
        //移动镜头目标点
        sightTarget.transform.Translate(deltaPosition * sensitivity);
        //调整瞄具倍率（默认倍率为2，所以缩放系数为 2/(sightSlider.value * 2) = 1/sightSlider.value ）
        float zoomNum = 1/sightSlider.value;
        gunController.zoom(zoomNum);
    }
}
